//
//  MapViewController.m
//  SelfDriving
//
//  Created by apple on 15/11/3.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "MapViewController.h"
#import "CustomAnnotationView.h"

@interface MapViewController ()<MAMapViewDelegate>
@property (weak, nonatomic) IBOutlet MAMapView *mapView;


@end

@implementation MapViewController
{
    int isload;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _mapView.delegate = self;
    _mapView.showsUserLocation = NO;
    CLLocationCoordinate2D coor;
    MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
    if ([_model isKindOfClass:[DTModel class]]) {
        DTModel *model = (DTModel *)_model;
        NSString * lat = model.lat;
        NSString *lon = model.lng;
        if(isload==0){
            coor.latitude = [lat floatValue];
            coor.longitude = [lon floatValue];
            _mapView.centerCoordinate = coor;
            isload = 1;
            pointAnnotation.title = [NSString stringWithFormat:@"%@",model.addr];
        }
    }else{
        coor.latitude =  _model.lat;
        coor.longitude =  _model.lng;
        _mapView.centerCoordinate = coor;
        pointAnnotation.title = [NSString stringWithFormat:@"%@",_model.address];
    }
    pointAnnotation.coordinate = coor;
    [_mapView addAnnotation:pointAnnotation];
}
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *reuseId = @"identifier";
        MAPinAnnotationView *an = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
        if (an==nil) {
            an=[[MAPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:reuseId];
        }
        //点击可以弹出气泡
        an.canShowCallout=YES;
        //自己设置标记点的图片，这里我用了一个分类来改变素材的大小
        UIImage *image = [UIImage imageNamed:@"ltgxtb"];
        UIView *bgView = [[UIView alloc]init];
        bgView.bounds = CGRectMake(0, 0, 200, 60);
        UILabel * label = [[UILabel alloc]init];
//        if (self.model.address) {
//
//        }else{
//            label.text = @"";
//        }
        DTModel *model = (DTModel *)self.model;
        //用户与用户发布位置后，点击位置查看，程序发生崩溃:原因去model.addr就unrecognized selector sent to instance(xhw11月24号改)
        if (self.locationType == 1 ) { //消息
            label.text = self.model.address;
        }
        else if (self.locationType == 2) { //动态
            label.text =  model.addr;
        }
//        label.text =  model.addr;
        //用户与用户发布位置后，点击位置查看，程序发生崩溃:原因去model.address就unrecognized selector sent to instance(xhw2018-1-17改bug15172但是之前的model.addr又找不到了，所有加了下面if)->注释掉了下面的一行代码，打开了上面一句代码
//        label.text = self.model.address;


        label.textAlignment = NSTextAlignmentCenter;
        label.frame = CGRectMake(0, 0, 200, 30);
        label.font = [UIFont systemFontOfSize:12];
//        [label sizeToFit];
        [bgView addSubview:label];
        UIImageView *img = [[UIImageView alloc]initWithImage:image];
        img.frame = CGRectMake(0, 30, 200, 30);
        img.contentMode = UIViewContentModeScaleAspectFit;
        [bgView addSubview:img];
        
//        CGSize imagesize = CGSizeMake(16, 22);
        CGSize imagesize = CGSizeMake(200, 60);
        
        
        
//        an.image = [image imageByScalingToSize:(imagesize)];
        an.image = [[UIImage qmui_imageWithView:bgView] imageByScalingToSize:(imagesize)];
        an.rightCalloutAccessoryView = label;
        
        
//        [mapView addSubview:label];
        
        return an;
    }
     return nil;
}
#pragma  mark 高德
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation
{
    if (updatingLocation)
    {
        NSString *lat;
        NSString *lon;
        if ([_model isKindOfClass:[DTModel class]]) {
            DTModel *model = (DTModel *)_model;
            lat = model.lat;
            lon = model.lng;
            if(isload==0){
                CLLocationCoordinate2D coor;
                coor.latitude = [lat floatValue];
                coor.longitude = [lon floatValue];
                _mapView.centerCoordinate = coor;
                isload = 1;
            }
        }else{
            if(isload==0){
                CLLocationCoordinate2D coor;
                coor.latitude =  _model.lat;
                coor.longitude =  _model.lng;
                _mapView.centerCoordinate = coor;
                isload = 1;
            }
        }
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (IBAction)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


@end
